import DataType from "@/utils/DataType";

let array = [];
const baseString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const len = baseString.length;

const RandomUtils = {
	randomNumber(minNum, maxNum) {
		const count = (maxNum - minNum) <= 10 ? (maxNum - minNum) : 100;
		let randomNum = 0;
		if (DataType.isNumber(minNum) && DataType.isNumber(maxNum)) {
			randomNum = parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
		} else if (DataType.isNumber(minNum)) {
			randomNum = parseInt(Math.random() * minNum + 1, 10);
		} else {
			randomNum = parseInt(Math.random() * count, 10);
		}
		if (count > 1 && array.includes(randomNum)) {
			this.randomNumber(minNum, maxNum);
		}
		array.push(randomNum);
		if (array.length > count) {
			array = [];
		}
		return randomNum;
	},

	randomString(length = 6) {
		let strings = [];
		for (let i = 0; i < length; i++) {
			let index = this.randomNumber(0, len - 1);
			strings.push(baseString.charAt(index));
		}
		return strings.join("");
	},

	randomDate(startDate = new Date("2020-01-01"), endDate = new Date()) {
		let days = parseInt((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
		return new Date(startDate.getTime() + this.randomNumber(0, days) * 24 * 60 * 60 * 1000);
	}
};
export default RandomUtils;
